package xmlele
import (
"encoding/xml"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlbuilder"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlnode"
)
type XMLEle struct {
xml .StartElement
tree .NSBuilder
Attrs []tree .Node
Children []tree .Node
Parent tree .Elem
tree .NodePos
tree .NodeType
}
func Root () xmlbuilder .XMLBuilder {
return &XMLEle {NodeType : tree .NtRoot }
}
func (x *XMLEle ) CreateNode (opts *xmlbuilder .BuilderOpts ) xmlbuilder .XMLBuilder {
if opts .NodeType == tree .NtElem {
ele := &XMLEle {
StartElement : opts .Tok .(xml .StartElement ),
NSBuilder : tree .NSBuilder {NS : opts .NS },
Attrs : make ([]tree .Node , len (opts .Attrs )),
Parent : x ,
NodePos : tree .NodePos (opts .NodePos ),
NodeType : opts .NodeType ,
}
for i := range opts .Attrs {
ele .Attrs [i ] = xmlnode .XMLNode {
Token : opts .Attrs [i ],
NodePos : tree .NodePos (opts .AttrStartPos + i ),
NodeType : tree .NtAttr ,
Parent : ele ,
}
}
x .Children = append (x .Children , ele )
return ele
}
node := xmlnode .XMLNode {
Token : opts .Tok ,
NodePos : tree .NodePos (opts .NodePos ),
NodeType : opts .NodeType ,
Parent : x ,
}
x .Children = append (x .Children , node )
return x
}
func (x *XMLEle ) EndElem () xmlbuilder .XMLBuilder {
return x .Parent .(*XMLEle )
}
func (x *XMLEle ) GetToken () xml .Token {
return x .StartElement
}
func (x *XMLEle ) GetParent () tree .Elem {
return x .Parent
}
func (x *XMLEle ) GetChildren () []tree .Node {
ret := make ([]tree .Node , len (x .Children ))
for i := range x .Children {
ret [i ] = x .Children [i ]
}
return ret
}
func (x *XMLEle ) GetAttrs () []tree .Node {
ret := make ([]tree .Node , len (x .Attrs ))
for i := range x .Attrs {
ret [i ] = x .Attrs [i ]
}
return ret
}
func (x *XMLEle ) ResValue () string {
ret := ""
for i := range x .Children {
switch x .Children [i ].GetNodeType () {
case tree .NtChd , tree .NtElem , tree .NtRoot :
ret += x .Children [i ].ResValue ()
}
}
return ret
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .